#ifndef __WPLAYER_CPP__
#define __WPLAYER_CPP__

#include "WExt/whead.h"

CPlayer* g_pSafePlayer;

CPlayer::CPlayer(int cindex)
{
	dbid = 0;
	clientindex = cindex;
	classname = "CPlayer";
}

CPlayer::CPlayer()
{
	dbid = 0;
	clientindex = 0;
	classname = "CPlayer";
}

CPlayer::~CPlayer()
{
}

CPlayer* getPlayerByCIndex(int clientindex)
{
	if (clientindex <= 0 || clientindex >= maxconn) return g_pSafePlayer;
	wserSocketData& socketdata = clientlist[clientindex];
	if (socketdata.objectid <= 0 || socketdata.objectid >= maxobject) return g_pSafePlayer;
	if (objectlist[socketdata.objectid].objectpoint == 0) return g_pSafePlayer;
	CObject* object = (CPlayer*)objectlist[socketdata.objectid].objectpoint;
	if (object->classname != "CPlayer") return g_pSafePlayer;
	return (CPlayer*)object;
}

int createPlayer(int clientindex)
{
	int objectid = getnewobjectid();
	if (objectid == -1) return -1;
	CPlayer* player = new CPlayer(clientindex);
	clientlist[clientindex].objectid = objectid;
	player->objectid = objectid;
	objectlist[objectid].objectpoint = player;
	wserPrintMsg("create player. objectid:%d clientindex:%d\n", objectid, clientindex);
	addCallback(clientindex, TIMER_SYNCPOS);
	return objectid;
}

void removePlayer(int clientindex)
{
	CPlayer* player = getPlayerByCIndex(clientindex);
	if (player == g_pSafePlayer || player == 0) return;
	wserPrintMsg("remove player. objectid:%d clientindex:%d\n", player->objectid, player->clientindex);
	clientlist[clientindex].objectid = 0;
	objectlist[player->objectid].objectpoint = NULL;
	cancelCallBackPlayer(clientindex);
	delete player;
}

CPlayer* getSafePlayer()
{
	return g_pSafePlayer;
}

#endif //__WPLAYER_CPP__
